



#include <iostream>

#include "VinH264Decoder.h"
#include "VinH264PostDecoder.h"

using namespace std;

/*!
@function   PostDecoder.
@abstract   Default constructor to set the decoded filename.
@param      decodeFileName, the Name of the decoded file to be written.
@result     None.
*/
PostDecoder::PostDecoder (char* decodeFileName)
{
    fileName.assign(decodeFileName);
    outFile->open(fileName.c_str(), ios::out | ios::binary);
    outFile = NULL;
}


/*!
@function   ~PostDecoder.
@abstract   Default destructor to cleanup.
@param      None.
@result     None.
*/
PostDecoder::~PostDecoder ()
{
    if ( outFile->is_open() ) {
        outFile->close();
        delete outFile;
        outFile = NULL;
    }
}


/*!
@function   RegisterFrameDecodeCompleteCallback.
@abstract   This function registers the callback function to the VinH264Decoder class.
@param      decoder, The decoder instance.
@result     None.
*/
inline void PostDecoder::RegisterFrameDecodeCompleteCallback (VinH264Decoder* decoder)
{
    decoder->SubscribeDecodeCompleteEvent(decoderCallback);
}


/*!
@function   decoderCallback.
@abstract   Decoder function callback called after the decoder completes the decoding. the frames are written into the file.
@param      decProcData, the DecodedFrameList containing the decoded frames and the decoded frame count.
@param      inContext, the context passed at the time of regestring.
@result     None.
*/
static void decoderCallback (DecodedFrameList*    decProcData,
                             void*                inContext)
{
    PostDecoder* postDecoder = (PostDecoder*)inContext;

    for ( UInt32 bufferCount = 0; bufferCount < decProcData->bufferCount;
          bufferCount++ ) {
        postDecoder->writeFrame((UInt8*)decProcData->frameData->decodedFrame,
                                decProcData->frameData->decodedFrameLength);
    }
}


/*!
@function   writeFrame.
@abstract   Function to write the frame into the file.
@param      Frame, the decoded frame.
@param      Length, the framelength.
@result     None.
*/
void PostDecoder::writeFrame (UInt8* Frame,
                              UInt32 Length)
{
    outFile->write((char*)Frame, Length);
}